package dolf.objects.weapons.Dynamite;

import java.awt.Graphics2D;
import dolf.objects.Animation;
import dolf.objects.Ball;
import dolf.objects.CircleObject;
import dolf.objects.Hole;
import dolf.objects.ImageStore;
import dolf.objects.Pit;
import dolf.objects.Point;
import dolf.objects.Vector;
import dolf.objects.WorldObject;
import dolf.game.Level;
import dolf.game.Player;
import dolf.objects.weapons.Weapon;
import dolf.window.SystemMessagesHandler;

public class Dynamite extends CircleObject implements  Weapon, java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 300;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Dynamite.lang.lang").getString("name");
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Dynamite.lang.lang").getString("type");
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Dynamite.lang.lang").getString("description");
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/Dynamite/gfx/icon.png");

	private final int DEST_RAD=40;
	private java.util.LinkedList<String> imgVec = new java.util.LinkedList<String>();
	private java.util.LinkedList<String> bomb = new java.util.LinkedList<String>();
	private int bombstate;
	private int tricker;
	private Player pl;
	private Animation explosion; 
	private boolean throwen;
	private boolean inititalized = false;
	private boolean exploted;
	private int timer;
	
	public Dynamite(){
		super(null,12,false,true);
				
		//animation
		for (int i = 1; i <= 23; i++) {
			imgVec.add("dolf/gfx/environment/explosion/explosion"+i+".png");
		}
		explosion = new Animation(imgVec);
		for (int i = 0; i<= 7; i++){
			bomb.add("dolf/objects/weapons/Dynamite/gfx/animation"+i+".png");
		}
	}
	
	public void initialize(Player _pl) {
		if(this.inititalized){
			return;
		}
		this.inititalized = true;
		this.throwen = false;
		this.exploted = false;
		this.timer = 30;
		this.pl = _pl;
		this.pl.setWeaponState();
	}
	
	public void deInitialize(){
		this.inititalized = false;
		this.pl = null;
		this.throwen = false;
		this.exploted = false;
		this.timer = 30;
	}
	
	public void execute() {
		if(exploted){
			return;
		}
		if(throwen && timer == 0&& Level.getInstance().mousePressed()){
			double dist;
			Ball obj;
			for(int i = 0; i < Level.getInstance().getPlayers().size(); i++){
				obj = (Ball) Level.getInstance().getPlayers().get(i).getBall();
				if(obj.isVisible() && (dist = this.getPosition().getDist(obj.getPosition()))< this.DEST_RAD){
					obj.setMotion(ballReflectin(dist, obj));
				}				
			}
			this.explode();
			return;
		}
		if(this.throwen && this.timer != 0){
			this.timer--;			
		}
		if(!throwen && Level.getInstance().mousePressed()){
			this.setPosition(pl.getBall().getPosition().sub(pl.getBall().getMotion().setLength(this.getRadius()+pl.getBall().getRadius()+1)));
			this.setMotion(new Vector(0,0));
			if(throwable()){
				this.bombstate = 0;
				this.tricker = 5;
				Level.getInstance().registerObject(this);
				this.throwen = true;
				this.pl.setWeaponState();
			} else {
				System.out.println("abwurf nicht moegl.");
			}	
		}
	}

	public void remove() {
		double dist;
		Ball obj;
		if(!exploted && this.throwen){
			for(int i = 0; i < Level.getInstance().getPlayers().size(); i++){
				obj = (Ball) Level.getInstance().getPlayers().get(i).getBall();;
				if(obj.isVisible() && (dist = this.getPosition().getDist(obj.getPosition()))< this.DEST_RAD){
					obj.setMotion(ballReflectin(dist, obj));
				}		
			}
			System.out.println("remove");
			this.explode();
		}
		pl.removeWeapon(this);
		pl.setAktWeapon(null);
	}
	
	public boolean newMotion(WorldObject object){
		boolean ret = true;
		if(!exploted){
			double dist;
			Ball obj;
			for(int i = 0; i < Level.getInstance().getPlayers().size(); i++){
				obj = (Ball) Level.getInstance().getPlayers().get(i).getBall();
				if(obj.isVisible() && (dist = this.getPosition().getDist(obj.getPosition()))< this.DEST_RAD){
					Vector vec;
					if((vec = ballReflectin(dist, obj)) == null){
						if(obj == object){
							ret = false;
						}
					} else {
						obj.setMotion(vec);
					}
					
				}
			}
			System.out.println("newMotion");
			this.explode();
		}
		return ret;
	}
	
	public String getDescription() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}
	public int getPrice() {
		return Price;
	}
	public String getType(){
		return WeaponType;
	}
	public javax.swing.ImageIcon getIcon() {
		return Icon;//ImageStore.getInstance().getIcon("dolf/objects/weapons/Dynamite/gfx/icon.png");	
	}
	
	public void paint(Graphics2D g){
		if(this.tricker <= 0){
			this.tricker = 5;
			this.bombstate = (++this.bombstate)%7;
		}
		this.tricker--;
		g.drawImage(ImageStore.getInstance().getImage(bomb.get(bombstate)), this.getPosition().getIntX()-this.getRadius(), this.getPosition().getIntY()-this.getRadius(), null);
	}
	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}

	public Player getPlayer() {
		return this.pl;
	}

	private void explode(){
		Level.getInstance().removeObject(this);
		this.setCollitabe(false);
		//load animation of explosion
		System.out.println("BBBBBBOOOOOOOOOOMMMMMM");
		explosion.startAt(new Point(this.getPosition().getIntX()-27, this.getPosition().getIntY()-55), false);
		Pit pit = new Pit(this.getPosition().getCopy(),25, 0.0007);
		Level.getInstance().registerGround(pit);
		this.exploted = true;
	}
	// ball ist in der naehe der explosion
	private Vector ballReflectin(double dist, Ball obj){
		double destr = (this.DEST_RAD*2-dist);
		this.pl.addDamage((int)destr);
		obj.getPlayer().setHP((int) (obj.getPlayer().getHP() - destr));
		
		if(obj.getPlayer().getHP() == 0){
			return null;
		}
		Vector tmp = obj.getPosition().sub(this.getPosition());
		obj.setMotion(tmp.setLength(5));
		return tmp.setLength(5);
	}
	public double getState(){
		if(this.throwen){
			return 0;
		}else{
			return 1;
		}
	}
	
	private boolean throwable(){
		for(int i = 0; i < Level.getInstance().getObjects().size();i++){
			WorldObject obj = Level.getInstance().getObjects().get(i);
			if(obj != pl.getBall() && this.kollision(obj)==0 ){
				return false;
			}
		}
		for(int i = 0; i< Level.getInstance().getHoles().size();i++){
			Hole obj = Level.getInstance().getHoles().get(i);
			if(this.kollision(obj)==0 ){
				return false;
			}
		}
		return true;
	} 

}
